import { ThunkAction } from "redux-thunk";
import { rootStateType } from "@/store";
import { reqProductCollections } from "@/api/tourists";
export const UPDATE_RECOMMEND_LIST = "FETCH_RECOMMEND_LIST";
export const UPDATE_RECOMMEND_LOADING = "UPDATE_RECOMMEND_LOADING";

interface FetchRecommendListActionType {
  type: typeof UPDATE_RECOMMEND_LIST;
  recommendList: any;
}
interface FetchRecommendLoadingActionType {
  type: typeof UPDATE_RECOMMEND_LOADING;
  loading: boolean;
}
export type RecommendProductsListActionType =
  | FetchRecommendListActionType
  | FetchRecommendLoadingActionType;
const updateRecommendList = (recommendList): FetchRecommendListActionType => ({
  type: UPDATE_RECOMMEND_LIST,
  recommendList,
});
const updateRecommendLoading = (loading): FetchRecommendLoadingActionType => ({
  type: UPDATE_RECOMMEND_LOADING,
  loading,
});
//请求热门推荐
export const fetchRecommendListAction =
  (): ThunkAction<
    void,
    rootStateType,
    unknown,
    RecommendProductsListActionType
  > =>
  async (dispatch, getState) => {
    // dispatch(updateRecommendLoading(true))
    let { data } = await reqProductCollections();
    if (data) {
      dispatch(updateRecommendList(JSON.parse(data)));
      dispatch(updateRecommendLoading(false));
    }
  };
